﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Transactions;
using Tebbee.Business.Server.Model;

namespace Tebbee.Business.Server.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ExchangeSer" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select ExchangeSer.svc or ExchangeSer.svc.cs at the Solution Explorer and start debugging.
    public class ExchangeSer : IExchangeSer
    {
        private readonly TebbeeContext context;

        public ExchangeSer()
        {
            context = new TebbeeContext();
        }

        //public ICollection<ExchangeLog_List_Result> ListExchanges(int startRow, string userName)
        //{
        //    return context.ExchangeLog_List(startRow, userName).ToList();
        //}

        public string AddNewShoppingCart(List<MemberExchangeLog> productList)
        {
            String error = "";
            ObjectParameter uid = new ObjectParameter("MemId", typeof(Int32));
            context.Exchangelog_GetMemIdByUsername(productList[0].UserName, uid);
            int memid = int.Parse(uid.Value.ToString());
            using (TransactionScope scope = new TransactionScope())
            {
                ObjectParameter returnId = new ObjectParameter("ReturnId", typeof(Int32));
                context.MemOrder_AddNewOrder(memid, returnId);
                int orderId = int.Parse(returnId.Value.ToString());
                foreach (MemberExchangeLog product in productList)
                {
                    ObjectParameter errorMessage = new ObjectParameter("ErrorMessage", typeof(String));
                    context.Exchangelog_AddNewOrderDetail(orderId, product.PId, product.QuantityToOrder, errorMessage);
                    if (!errorMessage.Value.ToString().Equals(""))
                    {
                        error = product.PId + " is out of stock !";
                    }
                }
                if (error.Equals(""))
                {
                    scope.Complete();
                }
            }
            return error;
        }

        public ICollection<MemberOrderViewModel> ListAllOrder(int startRow, string searchString, string searchDate, int status, int type)
        {
            var orders = new List<MemberOrderViewModel>();
            if (searchDate == "")
            {
                context.MemOrder_List(startRow, searchString, null, status, type).ToList().ForEach(e => orders.Add(new MemberOrderViewModel()
                {
                    OrderId = e.OrderId,
                    MemId = e.MemId,
                    DateExchange = e.DateExchange,
                    Status = e.Status,
                    UserName = e.UserName
                }));
            }
            else
            {
                context.MemOrder_List(startRow, searchString, searchDate, status, type).ToList().ForEach(e => orders.Add(new MemberOrderViewModel()
                {
                    OrderId = e.OrderId,
                    MemId = e.MemId,
                    DateExchange = e.DateExchange,
                    Status = e.Status,
                    UserName = e.UserName
                }));
            }
            foreach (MemberOrderViewModel item in orders)
            {
                var orderDetail = new List<MemberExchangeLog>();
                item.OrderDetail = new List<MemberExchangeLog>();
                context.Exchangelog_ListOrderDetailByOrderId(item.OrderId).ToList().ForEach(e => item.OrderDetail.Add(new MemberExchangeLog()
                {
                    ExLogId = e.ExLogId,
                    PId = e.PId,
                    Name = e.Name,
                    Score = e.Score,
                    ProductImg = e.ProductImg,
                    QuantityToOrder = e.Quantity
                }));
            }

            return orders;
        }



        public string UpdateOrderStatus(MemberOrderViewModel order)
        {
            ObjectParameter result = new ObjectParameter("Result", typeof(String));
            context.MemOrder_UpdateStatus(order.OrderId, order.Status, result);

            return result.Value.ToString();
        }


        public MemberFull GetMemberByUsername(string username)
        {
            MemberFull member = new MemberFull();
            member = context.Member_GetByUserName(username).Select(e => new MemberFull
            {
                FirstName = e.FirstName,
                LastName = e.LastName,
                Email = e.Email,
                Point = e.Point,
                CMND = e.CMND
            }).Single();

            return member;
        }
    }
}
